package br.com.j7.jdesk.ui.bean;

import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import br.com.j7.jdesk.persistence.entity.ServicesSituations;
import br.com.j7.jdesk.persistence.entity.ServicesStates;
import br.com.j7.jdesk.persistence.repository.ServiceSituationRepository;
import br.com.j7.jdesk.persistence.repository.ServiceStateRepository;
import br.com.j7.jdesk.persistence.repository.impl.ServiceSituationRepositoryImpl;
import br.com.j7.jdesk.persistence.repository.impl.ServiceStateRepositoryImpl;

/**
 * 
 * @author Edivando Alves
 *
 */
@ManagedBean
@RequestScoped
public class ServiceSituationBean extends WebContext implements Bean{

	private ServicesSituations serviceSituation = new ServicesSituations();
	private List<ServicesSituations> serviceSituations;
	private List<ServicesStates> servicesStates;
	
	private ServiceSituationRepository repository;
	private ServiceStateRepository serviceStateRepository;
	
	public ServiceSituationBean() {
		repository  = new ServiceSituationRepositoryImpl();
		serviceStateRepository = new ServiceStateRepositoryImpl();
		
	}
	
	@Override
	public String adicionar() {
		try {
			serviceSituation.setEnterprise(getSessionEnterprise());
			if(serviceSituation.getIdServiceSituation() == null || serviceSituation.getIdServiceSituation().equals(0)){
				serviceSituation.setIdServiceSituation(null);
				repository.add(this.serviceSituation);
				addMessage("ui.mobile.service.situation.add",serviceSituation.getCdServiceSituation() );
			}else {
				repository.update(this.serviceSituation);
				addMessage("ui.mobile.service.situation.update",serviceSituation.getCdServiceSituation());
			}
			serviceSituation = new ServicesSituations();
			serviceSituations = null;
			return "serviceSituation";
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}


	@Override
	public String remover() {
		try {
			if( repository.remove(serviceSituation) ){
				addMessage("ui.mobile.service.situation.remove",serviceSituation.getCdServiceSituation());
				serviceSituations = null;
				serviceSituation = null;
			}
			return "serviceSituation";
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public List<ServicesSituations> getServiceSituations() {
		try {
			if(this.serviceSituations == null){
				this.serviceSituations = repository.listByEnterprise(getSessionEnterprise());
			}
			return serviceSituations;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public ServicesSituations getServiceSituation() {
		return serviceSituation;
	}
	
	public void setServiceSituation(ServicesSituations serviceSituation) {
		this.serviceSituation = serviceSituation;
	}
	
	public List<ServicesStates> getServicesStates() {
		try {
			if(servicesStates == null){
				servicesStates = serviceStateRepository.list();
			}
			return servicesStates;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public String toForm(){
		return "serviceSituationForm";
	}
	
	@Override
	public String toList() {
		serviceSituation = new ServicesSituations();
		return "serviceSituation";
	}

}
